package iscript.coreFramework.data
{
	import flash.utils.Dictionary;
	import iscript.coreFramework.data.datatable.DataTable;
	import iscript.coreFramework.data.json.JSON;
	import iscript.coreFramework.util.ObjectUtil;
	import iscript.coreFramework.util.StringUtils;
	public class DataTool 
	{
		
		public function DataTool(){}
		
		
		static public function XMLToDataTable(data:XML):DataTable {
			 if (data.Column.name.length() == 0) return null;
		     var  _columnNames:Vector.<String> = new Vector.<String>();
			  for (var l:int = 0; l <data.Column.name.length(); l++) 
			  {
				  _columnNames.push(data.Column.name[l])
			  }
			  
			 var  _DataTable:DataTable = new DataTable(_columnNames);
				  _DataTable.name = data.@name
			 for (var i:int = 0; i < data.Row.data.length(); i++) {
				     var d:Dictionary = new Dictionary();
					 for (var m:int = 0; m <data.Row.data[i].attributes().length(); m++) 
					 {
						d[data.Row.data[i].attributes()[m].name().toString()] = data.Row.data[i].attributes()[m].toString();
					 }
					 _DataTable.addRow(d);
				}
			 return _DataTable;
		}
		
		static public function JsonToDataTable(jsonString:String):DataTable{
		    var  _json:* = iscript.coreFramework.data.json.JSON.decode(jsonString);
				if (_json.rows.length > 0) {
					  var  _columnNames:Vector.<String> =  ObjectUtil.getKeys(_json.rows[0]);
					  var  _DataTable:DataTable = new DataTable(_columnNames);
						   _DataTable.name = _json.name;					
					   for (var i:int = 0; i < _json.rows.length; i++) {
								var d:Dictionary = new Dictionary();
								 for (var m:int = 0; m <_columnNames.length; m++) 
								 {
									 d[_columnNames[m]] = _json.rows[i][_columnNames[m]];
								 }
								 _DataTable.addRow(d);
						   }
					  return _DataTable;
					}
				return null;
		}
		
		
		static public function CSVToDataTable(csv:String,tableName:String=null):DataTable {
			   var arr:Array = csv.split("\r\n");//换行
			   if (arr.length == 0) return null;
			   var columnArray:Array = String(arr[0]).split("	");
			   var column:Vector.<String> = new Vector.<String>();
			   for (var i:int = 0; i <columnArray.length; i++) 
			   {
				   column.push(StringUtils.trim(columnArray[i].toString()));
			   }
			   
			   var table:DataTable = new DataTable(column);
			   for (var j:int = 1; j < arr.length-1; j++) 
			   {
				   var currentValue:Array = String(arr[j]).split("	");
				   var value:Dictionary = new Dictionary();
				   for (var k:int = 0; k < column.length; k++) 
				   {
					  value[column[k]] = currentValue[k];   
				   }
				   table.addRow(value);
			   }
			   if (tableName != null) table.name = tableName;
			   return  table;
			}
		
		
		//{=====Filter 解析部分==============================
		/*var filterdata:Object = {
								//age: { ">=":5, "<=":10 },
								age:[5,7], 
								name:{"like":"_"}
								 // columnName:{"=":1} 
				                }*/
		/**
		 * 根据条件判断数据
		 * @param	resoure
		 * @param   filterdata
		 * @return
		 */
		static public  function filter(resoure:*,filterdata:Object):Boolean {
			var filterArray:Array = _parse(filterdata);
			  for (var j:int = 0; j < filterArray.length; j++) {
				
				  if (filterArray[j][1] is Array) {  
					 //column:[value1,value2]
					if (!_ArrayHasValueMethond(filterArray[j][1], resoure[filterArray[j][0]])) {
						return false;
						}
					
					}else { 
					//column:{">",value}
				  if (!_OperatorMethond(resoure[filterArray[j][0]], filterArray[j][1], filterArray[j][2])) {
							return false;
							}
					}
				}
			return true;
			}	
			
		/**
		 * 解析条件队列到数组    Array 和 Object
		 * @param	filterdata
		 * @return
		 */
		static private function _parse(filterdata:Object):Array {
			var data:Array = new Array();			
			for ( var  item:* in filterdata) {
                if (filterdata[item] is Array) {	
					data.push([item,filterdata[item]])					
					}else{	
			      for (var $itemChild:* in filterdata[item]) {
					  //[column,>,value]
				       data.push([item,$itemChild,filterdata[item][$itemChild]])
				  }
			   } 				
			}
			return data;
			}
			
		/**
		 * 数组是存在值
		 * @param	data
		 * @param	value
		 * @return
		 */	
		static private function _ArrayHasValueMethond(data:Array, value:*):Boolean {
			    for (var i:int = 0; i < data.length; i++) {
					   if (data[i] == value) {
						   return true;
						   }
					}
			return false;
			}
			
		/**
		 * 运算符方法处理 Object条件
		 * @param	data
		 * @param	Operator
		 * @param	value
		 * @return
		 */
		static private  function _OperatorMethond(data:*,Operator:String,  value:*):Boolean {
			var b:Boolean = true; 
			if (data == null) { return false; }
			var $data:String= data.toString();
			switch (Operator) {
				 case ">":	
					if (!(data > value)) {  b = false; }			 
				 break;				 
				 case "<":	
					if (!(data < value)) { b = false;}
				 break;
				 case ">=":	
					if (!(data >= value)) { b = false;}
				 break;
				 case "<=":	
					if (!(data <= value)) { b = false;}
				 break;
				 case "=":	
					if (!(data == value)) { b = false;}
				 break;
				 case "!=":	
					if (!(data != value)) { b = false;}
				 break;
				 case "like": //含有
					 if ($data.indexOf(value)==-1) { b = false;}
				 break;
				 case "^":	 //含有 
					 if ($data.indexOf(value)==-1) { b = false;}
				 break;
				  case "^=":  //开头等于 
					 if ($data.indexOf(value)!=0) { b = false;}
				 break;				 
				 }				 
			return b;
			}
		//}===================================
		
	}

}